package com.etoak.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class RedisService {

    @Autowired
    StringRedisTemplate redisTemplate;

    public void setex(String key, String value, long expire, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void hset(String key,String field ,String value) {
        redisTemplate.opsForHash().put(key, field, value);

    }

    public List<String> hvals(String key) {
        return redisTemplate.opsForHash().values(key)
                .stream().map(Object::toString).toList();
    }

    public void hdel(String key,String... fields) {
        redisTemplate.opsForHash().delete(key, fields);
    }

}
